package com.junzhiit.nengji.cms.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.junzhiit.nengji.cms.rabbitmq.RabbitMQService;
import com.junzhiit.nengji.cms.service.BankCardService;
import com.junzhiit.nengji.cms.service.BookingService;
import com.junzhiit.nengji.cms.service.CommissionHistoryService;
import com.junzhiit.nengji.cms.service.CouponsService;
import com.junzhiit.nengji.cms.service.FullReductionService;
import com.junzhiit.nengji.cms.service.GoodsRefundService;
import com.junzhiit.nengji.cms.service.MemberCardService;
import com.junzhiit.nengji.cms.service.NoticeService;
import com.junzhiit.nengji.cms.service.OrdersGoodsService;
import com.junzhiit.nengji.cms.service.OrdersRefundService;
import com.junzhiit.nengji.cms.service.OrdersService;
import com.junzhiit.nengji.cms.service.ShopClientService;
import com.junzhiit.nengji.cms.service.ShopService;
import com.junzhiit.nengji.cms.service.UserCouponsService;
import com.junzhiit.nengji.cms.service.UserEvaluateService;
import com.junzhiit.nengji.cms.service.WechatFlowHistoryService;
import com.junzhiit.nengji.cms.service.WithdrawalHistoryService;
import com.junzhiit.nengji.dao.UserVipDAO;
import com.junzhiit.nengji.dao.cache.PrinterCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;


@Configuration
@EnableScheduling
public class SchedulingConfig {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserEvaluateService userEvaluateService;
	
	@Autowired
	private OrdersService ordersService;
	
	@Autowired
	private WechatFlowHistoryService wechatFlowHistoryService;
	
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private CouponsService couponsService;
	
	@Autowired
	private UserCouponsService userCouponsService;
	
	@Autowired
	private ShopClientService shopClentService;
	
	@Autowired
	private MemberCardService memberCardService;
	
	@Autowired
	private NoticeService noticeService;
	
	@Autowired
	private BankCardService bankCardService;
	
	@Autowired
	private CommissionHistoryService commissionHistoryService;
	
	@Autowired
	private OrdersGoodsService ordersGoodsService;
	
	@Autowired
	private WithdrawalHistoryService withdrawalHistoryService;
	
	@Autowired
	private RabbitMQService rabbitMQService;
	
	@Autowired
	private GoodsRefundService goodsRefundService;
	
	@Autowired 
	private OrdersRefundService ordersRefundService;
	
	@Autowired
	private PrinterCacheDAO printerCacheDAO;
	
	@Autowired
	private BookingService bookingService;
	
	@Autowired
	private UserVipDAO userVipDAO;
	
	@Autowired
	private FullReductionService fullReductionService;
	
	
	@Scheduled(cron = "0 0 4 * * ?")
	public void defaultEvaluate(){
		//7天默认评价
		logger.info("run...");
		userEvaluateService.defaultEvaluate();
		logger.info("end...");
		
	}
	
	@Scheduled(cron = "0 0 3 * * ?")
	public void defaultRecerve(){
		//7天默认确认收货
		logger.info("run...");
		ordersService.defaultRecerve();
		logger.info("end...");
		
	}
	
	@Scheduled(cron = "0 15 3 * * ?")
	public void joinProperty(){
		//普通商品确认收货后7天加入资产
		logger.info("joinProperty run...");
		ordersService.joinProperty();
		logger.info("end...");
		
	}
	
	@Scheduled(cron = "0 30 3 * * ?")
	public void sendProperty(){
		//配送订单一天后进入资产
		logger.info("run...");
		ordersService.confirmOrders();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0 2 * * ?")
	public void makeFailure(){
		//公告自动失效
		logger.info("run...");
		noticeService.makeFailure();
		logger.info("end...");
		
	}
	
	@Scheduled(cron = "0 0 1 * * ?")
	public void orderUpdate(){
		//超过24小时的订单更新为无效
		logger.info("update orders status run...");
		ordersService.updateStatus();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0 9 * * ?")
	public void updateFLowHistory(){
		//获取小程序流量信息
		logger.info("acquire flowHistory info run...");
		wechatFlowHistoryService.flowHistory();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0 0 * * ?")
	public void updateShopExpireTime(){
		//把过期的店铺修改为停业整顿，且审核状态改为审核不通过
		logger.info("update shopStatus run...");
		shopService.updateShopExpireTime();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0/1 * * * ?")
	public void soldOutCoupons(){
		//下架超时优惠券
		logger.info("update couponsStatus run...");
		couponsService.soldOutCoupons();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0/1 * * * ?")
	public void soldOutUserCoupons(){
		//下架用户超时的优惠券
		logger.info("update userCouponsStatus run...");
		userCouponsService.soldOutUserCoupons();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0 1 * * ?")
	public void updateMemberExpireTime(){
		//修改会员卡过期的会员状态
		logger.info("update memberClientStatus run...");
		shopClentService.updateMemberExpireTime();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0 1 * * ?")
	public void updateMemberCardStatus(){
		//修改会员卡过期的状态
		logger.info("update memberCardStatus run...");
		memberCardService.updateMemberCardStatus();
		logger.info("end...");
	}
	
/*	@Scheduled(cron = "0 0 1 * * ?")
	public void joinRebate(){
		//免单商品(查询7天前的订单，加入排队返利)
		logger.info("join rebate run...");
		rebateService.joinRebate();
		logger.info("end...");
	}*/
	
	@Scheduled(cron = "0 0 1 * * ?")
	public void addSell(){
		//分销
		logger.info("add sell run...");
		commissionHistoryService.countCommission();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0 11 * * ?")
	public void sendRemindNote(){
		//发送合同即将到期提醒短信
		logger.info("send note run...");
		shopService.sendRemindInfo();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 0 11 * * ?")
	public void sendRemindExpireNote(){
		//发送店铺到期提醒短信
		logger.info("send expire note run...");
		shopService.sendRemindExpireInfo();
		logger.info("end...");
	}
	
	@Scheduled(cron = "0 30 0 * * ?")
	public void updateCheckCount(){
		//每日更新所有用户及商户的银行卡检测次数
		logger.info("update check count run...");
		bankCardService.updateCheckCount();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0 0 1 * ?")
	public void updateMonthIncomeMoney(){
		//每月月初凌晨更新当月收入金额
		logger.info("update monthIncomeMoney run...");
		withdrawalHistoryService.updateMonthIncomeMoney();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0/2 * * * ?")
	public void retrySendFailureMessage(){
		//每2分钟重发失败的消息记录
		rabbitMQService.retrySendFailureMessage();
	}
	
	@Scheduled(cron = "0 0 3 * * ?")
	public void updateCanRefund(){
		//确认收货七天后更改无法退货/退款
		logger.info("update can refund run...");
		ordersGoodsService.updateCanRefund();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0 1 * * ?")
	public void updateGoodsRefundAgree(){
		//每日凌晨1点更新商家未处理到期退货申请
		logger.info("update goodsRefund about agree run...");
		goodsRefundService.updateGoodsRefundAgree();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0 1 * * ?")
	public void updateGoodsRefundComplete(){
		//每日凌晨1点更新商家未处理到期退货确认
		logger.info("update goodsRefund about complete run...");
		goodsRefundService.updateGoodsRefundComplete();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0 1 * * ?")
	public void updateOrdersRefundComplete(){
		//每日凌晨1点更新商家未处理到期退款申请
		logger.info("update ordersRefund about complete run...");
		ordersRefundService.updateOrdersRefundComplete();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0 1 * * ?")
	public void deleteInvalid(){
		//每日凌晨1点清除user_vip表逾期的数据
		logger.info("delete user_vip invalid data  about complete run...");
		userVipDAO.deleteInvalid();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0 1 * * ?")
	public void updateDailyOrderNumber(){
		//每日凌晨1点更新每日订单编号
		logger.info("update dailyOrderNumber about complete run...");
		String dailyOrderNumberKey = DIYKey.getDailyOrderNumber();
		printerCacheDAO.addString(dailyOrderNumberKey, "");
		ordersRefundService.updateOrdersRefundComplete();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 0/1 * * * ?")
	public void updateValidTimeStatus(){
		//每分钟更新预订桌号过期失效订单
		logger.info("update validTimeStatus about complete run...");
		bookingService.updateStatusByValidTime();
		logger.info("end...");
	}
	
	@Scheduled(cron="0 22 17 * * ?")
	public void updateFullReductionStatus(){
		//每日凌晨2点更新满减活动状态
		logger.info("update fullReductionStatus about complete run...");
		fullReductionService.updateFullReductionStatus();
		logger.info("end...");
	}
}
